layui.define(['layer', 'form', 'laydate'], function (exports) {
    var MOD_NAME = 'getHtml', $ = layui.jquery, form = layui.form, table = layui.table, layer = layui.layer;
    var laydate = layui.laydate;
    getHtml = function () {
    };
    getHtml.prototype.config = function () {
        return {}
    }

    getHtml.prototype.init = function (e) {
        $("#" + e.id + "_form_box").find("[lay-filter='get_select_option']").each(function (index, item) {
            var id_name = "#" + $(item).attr("id");
            getHtml.set_select({
                id: id_name
            });
        });
        $("#" + e.id + "_form_box").find("[lay-filter='show_date']").each(function (index, item) {
            var id_name = "#" + $(item).attr("id");
            laydate.render({
                elem: id_name
                , format: 'yyyy-MM-dd' //可任意组合
            });
        });

        $("#" + e.id + "_form_box").find("[lay-filter='show_day']").each(function (index, item) {
            var id_name = "#" + $(item).attr("id");
            laydate.render({
                elem: id_name
                , format: 'dd' //可任意组合
            });
        });

        $("#" + e.id + "_form_box").find("[lay-filter='show_time']").each(function (index, item) {
            var id_name = "#" + $(item).attr("id");
            laydate.render({
                elem: id_name
                , type: 'time'
                , format: 'HH:mm:ss' //可任意组合
                , done: function (value, date) {
                    console.log(value);
                }
            });
        });

        $("#" + e.id + "_form_box").find("[lay-filter='show_file_list']").each(function (index, item) {
            var file_list_json = JSON.parse($(item).attr("lay-data"));
            var html = "";
            var files_id_arr = [];
            layui.each(file_list_json, function (i, v) {
                html += getHtml.file_li(v);
                files_id_arr.push(v.id);
            });

            var show_file = $('.file_show_box');
            var files_id = files_id_arr.join(",");
            $("input[name='files_id']").val(files_id);
            show_file.append(html);
            getHtml.set_file_btn(show_file);
        })
    }

    getHtml.prototype.file_li = function (opt) {
        if (opt.type == 2) {
            var shtml = '<li id="ufview_opid" data-id="opid" data-url="opurl" style="background-image:url(opurl);">';
        } else {
            var shtml = '<li id="ufview_opid" data-id="opid" data-url="opurl">';
        }
        var shtml = shtml.replaceAll("opid", opt.id, shtml);
        var shtml = shtml.replaceAll("opurl", opt.url, shtml);
        if (opt.type == 1) {
            shtml += '<i class="fa fa-file ifont1"></i>';
        }
        shtml += '<i class="fa fa-window-close iclose"></i>';
        if (opt.type == 1) {
            shtml += '<span class="txt">' + opt.name + '</span>';
        }
        shtml += '<a class="layui-btn layui-btn-xs layui-bg-orange sl-btn">下载</a>';
        shtml += '<a class="layui-btn layui-btn-xs layui-bg-blue sr-btn">预览</a>';
        shtml += '</li>';
        return shtml;
    }

    getHtml.prototype.set_file_btn = function (liobj) {
        liobj.find('i.iclose').on('click', function (e) {
            var files_id = liobj.attr('data-id');
            var id = $(this).parent("li");
            layer.confirm('确认删除吗？', {
                title: '<i class="layui-icon layui-icon-delete"></i> 删除确认',
                btn: ['确认', '取消']
            }, function (index) {
                id.remove();
                var files = [];
                $(".file_show_box").find("li").each(function () {
                    files.push($(this).data("id"));
                });
                var files_str = files.join(",");
                $("input[name='files_id']").val(files_str);
                layer.close(index);
            });
        });

        liobj.find('a.sl-btn').on('click', function (e) {
            var obj = $(this).parent();
            var id = obj.attr('data-id');
            var ifw = $("#filedown");
            layer.msg('开始下载', {time: 5000});
            ifw.attr('src', '{:url("Files/down")}?id=' + id);
        });

        liobj.find('a.sr-btn').on('click', function (e) {
            var url = $(this).parent("li").attr("data-url");
            var style = $(this).parent("li").attr("style");
            if (style != undefined) {
                kaige.viewimage({maxwidth: 600, maxheight: 600, url: url});
            } else {
                var d = {};
                d.url = url;
                viewFiles(d);
            }
        });
    }


    getHtml.prototype.select_html = function (str, is_json = true) {
        if (is_json) {
            var json = JSON.parse(str);
        } else {
            json = str;
        }
        console.log(json);
        var html = "<option value='0'>---请选择[带搜索]---</option>";
        $.each(json, function (index, item) {
            html += "<option value='" + item.id + "'>" + item.name + "</option>";
        });
        return html;
    }

    getHtml.prototype.set_select = function (e) {
        var str = $(e.id).attr("lay-data");
        var url = $(e.id).attr("lay-url");
        var html = '';
        if (str != undefined) {
            html = getHtml.select_html(str, true);
            $(e.id).append(html);
        }
        if (url != undefined) {
            $.get(url, {}, function (res) {
                if (res.status) {
                    html = getHtml.select_html(res.data, false);
                    var v = $(e.id).attr("lay-value");
                    $(e.id).append(html);
                    if (v != undefined) {
                        $(e.id).find("option[value='" + v + "']").attr("selected", "true");
                    }
                    form.render("select", "kaige_form_lay_filter");
                }
            });
        }
        var v = $(e.id).attr("lay-value");
        if (v != undefined) {
            $(e.id).find("option[value='" + v + "']").attr("selected", "true");
        }
    }
    getHtml.prototype.page = function (e) {
        $.get(e.url, e.data, function (res) {
            var content = '';
            if (Object.prototype.toString.call(res) === '[object Object]') {
                if (!res.status) {
                    layer.msg(res.msg);
                    return;
                }
                content = res.data;
            } else {
                content = res;
            }
            layer.open({
                id: e.id + '_form_box',
                type: 1,
                anim: 4,
                area: [e.width, e.height],
                title: e.title,
                maxmin: true,
                content: content,
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    $('#kaige_form_btn_save').click();
                },
                success: function (layero, index) {
                    if (e.overflow) $('#' + e.id).css('overflow', 'visible');
                    (typeof e.callbefore === 'function') && e.callbefore(layero, index);
                    getHtml.init(e);
                    (typeof e.changeaction === 'function') && e.changeaction(layero, index);
                    form.render(null, 'kaige_form_lay_filter');
                    form.on('submit(kaige_form_btn_save)', function (data) {
                        $.post(e.url, data.field, function (res2) {
                            if (res2.code == 0) {
                                layer.msg(res2.msg, {time: 1300}, function () {
                                    if ($("#" + e.id + "Table")) {
                                        table.reload(e.id + "Table");
                                    }
                                    layer.close(index);
                                    (typeof e.afterpost === 'function') && e.afterpost(layero, index);
                                });
                            } else {
                                layer.msg(res2.msg);
                            }
                        });
                        return false;
                    });
                }
            });

        }, 'html');

    }

    var getHtml = new getHtml();
    exports(MOD_NAME, getHtml);
});